//
//  GXHeaderViewController.m
//  拉取动画
//
//  Created by Onery on 2016/9/28.
//  Copyright © 2016年 Graliet. All rights reserved.
//

#import "GXHeaderViewController.h"

static NSString * const cellIdentifier = @"cellIdentifier";

@interface GXHeaderViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation GXHeaderViewController{
    UIView * _headView;
    UIImageView * _headImageView;
    UIView * _headLineView;
    UIStatusBarStyle _statusBarStyle;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self prepareTableView];
    [self prepareHearderView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.navigationController setNavigationBarHidden:YES];
}
//MARK: -头部视图
- (void)prepareHearderView {
    _statusBarStyle = UIStatusBarStyleLightContent;
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];
    _headView.backgroundColor = [UIColor hm_colorWithHex:0xF8F8F8];
    [self.view addSubview:_headView];
    
    _headImageView = [[UIImageView alloc] initWithFrame:_headView.bounds];
    _headImageView.backgroundColor = [UIColor hm_colorWithHex:0xF8F8F8];
    _headImageView.contentMode = UIViewContentModeScaleAspectFill;
    _headImageView.clipsToBounds = YES;
    NSURL * imageUrl = [NSURL URLWithString:@"http://www.who.int/entity/campaigns/immunization-week/2015/large-web-banner.jpg?ua=1"];
    [_headImageView sd_setImageWithURL:imageUrl];
    [_headView addSubview:_headImageView];
    
    
    CGFloat lineHeight = 1 / [UIScreen mainScreen].scale;
    
    NSLog(@"%f , %f",lineHeight,[UIScreen mainScreen].scale);
    _headLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 200 - lineHeight, _headView.hm_width, 5)];
    _headLineView.backgroundColor = [UIColor orangeColor];
    [_headView addSubview:_headLineView];
//    [_headView bringSubviewToFront:_headLineView];
    
    
    
}
//MARK: -tableView
- (void)prepareTableView {
    UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
    tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    tableView.scrollIndicatorInsets = tableView.contentInset;
    [self.view addSubview:tableView];
}

#pragma mark- dataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1000;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.textLabel.text = @(indexPath.row).stringValue;
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    NSLog(@"%f",scrollView.contentOffset.y + scrollView.contentInset.top);
    CGFloat offSet = scrollView.contentOffset.y + scrollView.contentInset.top;
//    NSLog(@"%f",offSet);
    if (offSet <= 0) {
//        NSLog(@"放大");
        _headView.hm_y = 0;
        _headView.hm_height = 200 - offSet;
        _headImageView.hm_height = _headView.hm_height;
    }else {
        _headImageView.hm_height = 200;
        _headView.hm_height = _headImageView.hm_height;
        CGFloat minY = 200 - 64;
        _headView.hm_y = -MIN(minY, offSet);
        CGFloat alapa = 1- (offSet / minY);
        _headImageView.alpha = alapa;
        _statusBarStyle = alapa > 0.5 ? UIStatusBarStyleLightContent:UIStatusBarStyleDefault;
        [self.navigationController setNeedsStatusBarAppearanceUpdate];
    }
    _headLineView.hm_y = _headView.hm_height;
    
}
-(void)dealloc {
    NSLog(@"-----------die------------");
}

-(UIStatusBarStyle)preferredStatusBarStyle {
    return _statusBarStyle;
}

@end
